/*
 * Chastoslov
 * Copyright (C) 2009 Alexandr Stroghov
 * 
 * stroghov@gmail.com
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License, version 2, as published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if 
 * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 * MA 02111-1307 USA
*/

package world.ah.wf.guiclient;
import java.io.Reader;
import java.util.HashSet;

/** 
 * Class that breaks a text into words.
 */
public class WordTokenizer {

  private String token = "";

  /** 
   * @return The last prepared token. Unknown behavior after call of nextToken() returns false.
   */
  public String getToken() {return token;}
  
  private Reader reader;
  private String st = "init";
  private char ch;
  private HashSet<Character> delims = new HashSet<Character>();
  
  public void addDelimeter(char ch) {
    delims.add(Character.valueOf(ch));
  }
  
  public void addDelimeters(char chFrom, char chTo) {
    for(char ch = chFrom; ch <= chTo; ch++) delims.add(Character.valueOf(ch));
  }
  
  private boolean contains(HashSet<Character> charList, char ch) {
    return charList.contains(Character.valueOf(ch));
  }
  
  /** 
   * Constructs a new tokenizer based on the given reader (stream of characters).
   */
  public WordTokenizer(Reader reader) {
    this.reader = reader;
  }
  
  private boolean readNextChar() throws Exception {
    int i;
    if((i = reader.read()) >= 0) {
      ch = (char)i;
      return true;
    }
    else return false;
  }
  
  /** 
   * Prepares next token if it's possible.
   * @return true if the token was prepared, false if no more tockens remained.
   */
  public boolean nextToken() throws Exception {

  token = "";
    while(readNextChar()) {
      //--------------------------------------------------
      if(st.equals("init")) {
        if(!contains(delims, ch)) {
          st = "word";
          token += String.valueOf(ch);
        }
      }
      //--------------------------------------------------
      else if(st.equals("word")) {
        if(contains(delims, ch)) {
          st = "init";
          return true;
        }  
        else {token += String.valueOf(ch);}
      }
      //--------------------------------------------------
      else throw new Exception("Unexpected error: Wrong state: " + st);
    }
    if(st.equals("init")) return false;
    else if(st.equals("word")) {st = "init"; return true;}
    else throw new Exception("Unexpected error: Wrong state: " + st);
  }

}